package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.filecontrol.service.DownloadService;
import com.lanou3g.ptp.filecontrol.service.PackedFileService;
import com.lanou3g.ptp.filecontrol.service.UploadService;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;

@Controller
public class FileController {


    @Reference
    private UploadService uploadService;


    @Reference
    private DownloadService downloadService;


    @Reference
    private PackedFileService packedFileService;



    public String fileUpload(MultipartFile file,String path) throws Exception {

        InputStream inputStream = file.getInputStream();
        uploadService.fileUpload(inputStream,path);

        return  null;
    };


    public OutputStream filedownload(Long taskId,Long studentId,String fileName){

        OutputStream outputStream = downloadService.filedownload(taskId,studentId,fileName);

        return outputStream;
    }


    @GetMapping("/getFile")
    public void PackedFile(HttpServletResponse response,Long taskId, Long studentId, String fileName) throws IOException {

        byte[] bytes = null;

        try {
             bytes = packedFileService.packedByStudent(1L,12L,"dabao");
        } catch (Exception e) {
            e.printStackTrace();
        }

        IOUtils.write(bytes,response.getOutputStream());
    }








}
